﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.SharePoint;
using System.Data;
using Microsoft.SharePoint.Utilities;
using SPPivotViewer.Utility;

namespace SPPivotViewer.HttpHandler.Internal
{
    public class SPSiteDataQueryFactory
    {
        public Collection MakeCollection(CollectionRequestContext context, string collectionFileName)
        {

            Collection collection = new Collection();
            string strListBase=context.Query["listbase"];
            if(string.IsNullOrEmpty(strListBase))
                return collection;
            SPSiteDataQuery query = new SPSiteDataQuery();
            string strMaxItem = PV4SPSettings.GetValue(SPContext.Current.Web, PV4SPSettings.cstMaxItem);
            uint _iMaxItem = 0;
            query.RowLimit = uint.TryParse(strMaxItem, out _iMaxItem) ? _iMaxItem : 300; 
            query.Webs = "<Webs Scope=\"Recursive\" />";
            query.Lists = string.Format("<Lists ServerTemplate=\"{0}\" />", strListBase);
            query.ViewFields = "<FieldRef Name=\"DocIcon\"  Nullable=\"TRUE\"/><FieldRef Name=\"ContentType\"  Nullable=\"TRUE\"/><FieldRef Name=\"Created\" Nullable=\"TRUE\"/><FieldRef Name=\"Title\" Nullable=\"TRUE\"/><FieldRef Name=\"Author\" Nullable=\"TRUE\"/><FieldRef Nullable=\"TRUE\" Name=\"FileRef\" /><FieldRef Nullable=\"TRUE\" Name=\"EncodedAbsUrl\" /><ProjectProperty Name=\"Title\" /><ListProperty Name=\"Title\" />";

            using (SPWeb oWeb = SPContext.Current.Web)
            {
                DataTable results = oWeb.GetSiteData(query);
                foreach (DataRow row in results.Rows)
                {
                    List<Facet> lstFct = new List<Facet>();
                    String strTitle=row["Title"].ToString();
                    String strUrl = row["EncodedAbsUrl"] + row["FileRef"].ToString().Substring(row["FileRef"].ToString().IndexOf("#") + 1);

                    lstFct.Add(new Facet("Site", FacetType.Text, row["ProjectProperty.Title"]));
                    lstFct.Add(new Facet("List", FacetType.Text, row["ListProperty.Title"]));
                    string valueFacet = new SPFieldUserValue(oWeb, row["Author"].ToString()).LookupValue;
                    lstFct.Add(new Facet("Author", FacetType.Text, valueFacet));
                    DateTime? dtValue = row["Created"] as DateTime?;
                    if (dtValue != null && dtValue.HasValue)
                        lstFct.Add(new Facet("Created", FacetType.DateTime, dtValue.Value));
                    lstFct.Add(new Facet("Doc Type", FacetType.Text, row["DocIcon"]));
                    lstFct.Add(new Facet("Content Type", FacetType.Text, row["ContentType"]));
                    if (strListBase.Equals("1"))
                        collection.AddItem(strTitle, strUrl, null, new ItemImage(new Uri(strUrl)), lstFct.ToArray());
                    else
                        collection.AddItem(strTitle, strUrl, null, new ItemImage(new Uri(IconForNonDocument(strUrl))), lstFct.ToArray());

                }
            }


            return collection;
        }
        private string IconForNonDocument(string strUrl)
        {
            string strIcon = SPUtility.MapToIcon(SPContext.Current.Web, strUrl, string.Empty, IconSize.Size32);

            string strIconPath = SPContext.Current.Web.Url + "/_layouts/images/" + strIcon;
            return strIconPath;
        }
    }
}
